package com.ruoyi.projects.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.constant.BuConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.projects.mapper.SlSysConfigMapper;
import com.ruoyi.projects.domain.SlSysConfig;
import com.ruoyi.projects.service.ISlSysConfigService;

/**
 * 系统配置Service业务层处理
 * 
 * @author yangz
 * @date 2024-12-01
 */
@Service
public class SlSysConfigServiceImpl implements ISlSysConfigService
{
    @Autowired
    private SlSysConfigMapper slSysConfigMapper;

    @Autowired
    private RedisCache redisCache;


    /**
     * 查询系统配置
     * 
     * @param id 系统配置主键
     * @return 系统配置
     */
    @Override
    public SlSysConfig selectSysConfigById(Long id)
    {
        return slSysConfigMapper.selectSysConfigById(id);
    }

    /**
     * 查询系统配置列表
     * 
     * @param slSysConfig 系统配置
     * @return 系统配置
     */
    @Override
    public List<SlSysConfig> selectSysConfigList(SlSysConfig slSysConfig)
    {
        return slSysConfigMapper.selectSysConfigList(slSysConfig);
    }

    /**
     * 新增系统配置
     * 
     * @param slSysConfig 系统配置
     * @return 结果
     */
    @Override
    public int insertSysConfig(SlSysConfig slSysConfig)
    {
        return slSysConfigMapper.insertSysConfig(slSysConfig);
    }

    /**
     * 修改系统配置
     * 
     * @param slSysConfig 系统配置
     * @return 结果
     */
    @Override
    public int updateSysConfig(SlSysConfig slSysConfig)
    {
        int i = slSysConfigMapper.updateSysConfig(slSysConfig);
        if (i>0){
            if (StringUtils.isNotEmpty(slSysConfig.getSwiperIndex())){
                slSysConfig.setSwiperIndexList(new ArrayList<String>(Arrays.asList(slSysConfig.getSwiperIndex().split(","))));
            }
            if (StringUtils.isNotEmpty(slSysConfig.getSwiperService())){
                slSysConfig.setSwiperServiceList(new ArrayList<String>(Arrays.asList(slSysConfig.getSwiperService().split(","))));
            }
            redisCache.setCacheObject(BuConstants.REDIS_SLSYSCONFIG_KEY,slSysConfig);
        }

        return i;
    }

    /**
     * 批量删除系统配置
     * 
     * @param ids 需要删除的系统配置主键
     * @return 结果
     */
    @Override
    public int deleteSysConfigByIds(Long[] ids)
    {
        return slSysConfigMapper.deleteSysConfigByIds(ids);
    }

    /**
     * 删除系统配置信息
     * 
     * @param id 系统配置主键
     * @return 结果
     */
    @Override
    public int deleteSysConfigById(Long id)
    {
        return slSysConfigMapper.deleteSysConfigById(id);
    }

    /**
     * 刷新redis配置信息
     */
    @Override
    public int updateRedisCache() {
        List<SlSysConfig> slSysConfigs = slSysConfigMapper.selectSysConfigList(new SlSysConfig());
        if (slSysConfigs.size()>0){
            SlSysConfig slSysConfig = slSysConfigs.get(0);
            if (StringUtils.isNotEmpty(slSysConfig.getSwiperIndex())){
                slSysConfig.setSwiperIndexList(new ArrayList<String>(Arrays.asList(slSysConfig.getSwiperIndex().split(","))));
            }
            if (StringUtils.isNotEmpty(slSysConfig.getSwiperService())){
                slSysConfig.setSwiperServiceList(new ArrayList<String>(Arrays.asList(slSysConfig.getSwiperService().split(","))));
            }
            redisCache.setCacheObject(BuConstants.REDIS_SLSYSCONFIG_KEY,slSysConfigs.get(0));
        }
        return 1;
    }
}
